package com.dayu.rabbitmq.transaction;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

import com.dayu.rabbitmq.util.MQUtil;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;

/**
 * @author dayu
 * @date 2019/02/20
 * @Describe
 */
public class TxSend {
    private static String QUEUE_NAME = "test_queue_tx";

    public static void main(String[] args) throws IOException, TimeoutException {
        Connection connection = MQUtil.getConnection();

        Channel channel = connection.createChannel();
        
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
//        channel.exchangeDeclare(QUEUE_NAME, "topic");

        String msg = "hello tx msg";

        try {
            channel.txSelect();
            channel.basicPublish("", QUEUE_NAME, null, msg.getBytes()); 
            int a=1/0;
            channel.txCommit();
            System.out.println("send " + msg);
        } catch (Exception e) {
            channel.txRollback();
            e.printStackTrace();
        }

        channel.close();
        connection.close();

    }
}
